package com.project.controller;

import com.github.pagehelper.PageInfo;
import com.project.bean.RoomBean;
import com.project.service.IRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("room")
public class RoomController {

    @Autowired
    private IRoomService service;

    @RequestMapping("findByItem")
    public PageInfo<RoomBean> findByItem(@RequestParam(value = "pageNo",defaultValue = "1") int pageNo,
           String site,String state, String canLive, String type){

        return service.findByItem(pageNo, site, state, canLive, type);
    }

    @RequestMapping("add")
    public String add(RoomBean room,@RequestParam("myFile") MultipartFile mf) throws IOException {

        String fileName = mf.getOriginalFilename();
        fileName = System.currentTimeMillis()+fileName;

        mf.transferTo(new File("D:/Project/webTest/webRoom/src/main/resources/static/html/img/"+fileName));
        room.setImage(fileName);
        service.addRoom(room);
        return "ok";
    }

    @RequestMapping("del")
    public String del(int id){
        service.delRoom(id);
        return "ok";
    }
    @RequestMapping("findById")
    public RoomBean findById(int id){
        return service.findById(id);
    }
}
